const db = require('mongodb');
const MongoClient = require("mongodb").MongoClient;
const config = require('./mongodb.config');
const fs = require('fs');
const jd = config.jd;

// const comments = {
//     url:"mongodb://localhost:27017/",
//     db:"JD_comments_test",
//     collection:"comments" 
// };
const updateMany = function (mongo, whereParams, object) {
    let params = {};
    params['$unset'] = {'refresh':null}
    return new Promise((resolve, reject) => {
      MongoClient.connect(mongo.url, { useNewUrlParser: true }, function (err, db) {
        if (err) reject([err, mongo, whereParams, object]);
        var dbo = db.db(mongo.db);
        var updateStr = { $set: object };
        dbo.collection(mongo.collection).updateMany(whereParams, params,{'upsert':true}, function (err, res) {
          db.close();
          if (err) reject(err);
          if (!res || !res.result) {reject([err, mongo, whereParams,object]);}
          else{
            resolve(`${mongo.collection}集合的` + res.result.nModified + " 条文档被更新");
          }
        });
      });
    });
  };


updateMany(jd.summary,{},{});